/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       dialog_campaign.cpp

	$Header: /game/dialog_campaign.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include <algorithm>
#include <fstream>
#include <strstream>

#include "dialog_campaign.h"

#include "adventure_frame.h"
#include "adventure_map.h"
#include "bitmap_group_cache.h"
#include "bitmap_layer_window.h"
#include "bitmap_layer_cache_window.h"
#include "bitmap_group_cache.h"
#include "bitmap_group_window.h"
#include "bound_handler.h"
#include "button.h"
#include "button_cache.h"
#include "difficulty_level.h"
#include "enum_operations.h"
#include "external_string.h"
#include "format_string.h"
#include "interface_help.h"
#include "item_help_block.h"
#include "main_menu.h"
#include "map_header.h"
#include "minimum_maximum.h"
#include "music.h"
#include "options.h"
#include "platform.h"
#include "replace_keywords.h"
#include "scenario_cut_scene_id.h"
#include "scenario_cut_scene_info.h"
#include "sound.h"
#include "sound_cache.h"
#include "standard_fonts.h"
#include "text_window.h"
#include "text_edit_window.h"


static t_bitmap_group_cache g_layout( "dialog.campaign" );
static t_bitmap_group_cache g_layout_epilogue( "dialog.campaign_epilogue" );

extern t_button_cache g_cancel_button;
extern t_button_cache g_large_button;


static t_bitmap_group_cache g_difficulty_icons( "icons.difficulty_60" );
static t_bitmap_group_cache g_map_size_icons( "icons.map_size" );

extern t_external_string const k_text_guards_move( "guards_move.shared" );
extern t_external_string const k_text_guards_dont_move( "guards_dont_move.shared" );

extern t_sound_cache const k_main_menu_music;

namespace
{
	char const * k_map_size_keywords[] = { "small_map", "medium_map", "large_map", "extra_large_map" };
	char const * k_map_size_icon_names[] = { "small", "medium", "large", "x_large" };

	int g_old_music_volume = 0;

	t_bitmap_group_cache & get_cache( t_scenario_cut_scene_image_id id )
	{
		static t_owned_ptr<t_bitmap_group_cache> cache_ptrs[k_scenario_cut_scene_image_count];

		if( cache_ptrs[id].get() == 0 )
		{
			std::string		text;
			text = "campaign_splashscreens.426x340.";
			text += get_resource_name( id );

			cache_ptrs[id].reset( new t_bitmap_group_cache( text ) );
		}

		return *cache_ptrs[id];
	}

}

int const k_onscreen_start_offset = 20;

// ---------------------------------------------------------------------
// score dialog
// ---------------------------------------------------------------------
t_scenario_dialog::t_scenario_dialog( t_window* parent, t_campaign_file_header_ptr const header_ptr, 
									 int map_selected, bool is_prologue, t_adventure_map* map )
				: t_window( k_completely_transparent, parent ),
					m_difficulty( k_difficulty_normal ),
					m_parent( parent ), m_paused( false ),
					m_file_header_ptr( header_ptr ),
					m_map_selected( map_selected ),
					m_guards_can_move( true ),
					m_is_prologue( is_prologue ),
					m_adventure_map( map ), 
					m_suspended ( false )
				  
{
	m_voice_window = NULL; 
	m_sound = NULL;
	initialize();
}

extern t_external_string const k_difficult_balloon_help[];
extern t_external_string const k_difficulty_help[];

void t_scenario_dialog::set_difficulty( t_difficulty difficulty )
{
	m_player_difficulty_window->set_frame( k_difficulty_icon_names[difficulty] );
	m_player_difficulty_window->set_help_balloon_text( k_difficult_balloon_help[ difficulty ] );
	m_player_difficulty_window->set_right_click_text( k_difficulty_help[ difficulty ] );
	m_difficulty_window->set_text( k_difficult_balloon_help[difficulty] );

	if( m_difficulty_left_button )
	{
		if( m_difficulty == k_difficulty_easy )
			m_difficulty_left_button->enable( false );
		else
			m_difficulty_left_button->enable( true );
	}

	if( m_difficulty_right_button )
	{
		if( m_difficulty == k_difficulty_impossible )
			m_difficulty_right_button->enable( false );
		else
			m_difficulty_right_button->enable( true );
	}
}

        

// ------------------------------------------------
// dialog to select a new map.
// add a tab
// ------------------------------------------------
t_toggle_button* t_scenario_dialog::add_tab( const char* tab_name, std::string const& label, 
							    std::string const& help_text,
								bool pressed )
{							
	t_toggle_button*		tab;
	t_window*				child;
	t_text_window*			text_window;
	t_screen_rect			tab_rect;
	t_bitmap_layer const*	layer;
	std::string				layer_name = tab_name;
	t_screen_point			location;
	t_help_block const&		campaign_help = get_help_block( "campaign" );

	layer = m_layout->find( (layer_name + "_pressed").c_str() );
	if( !layer )
		return 0;
	tab_rect = layer->get_rect();
	location = tab_rect.top_left();
	tab_rect -= location;
	tab_rect.top = 14;
	tab = new t_toggle_button( location, this, true );

	t_cached_font         font = get_font( tab_rect.height() - 10 );

	child = new t_bitmap_layer_window( layer, -location, tab );
	text_window = new t_text_window( font, tab_rect, child, label.c_str(),
		                             t_pixel_24(0,0,0) );
	text_window->set_center_horizontal();
	text_window->set_center_vertical();
	text_window->set_drop_shadow( true, t_pixel_24( 200, 200, 200 ));
	tab->set_pressed_image( child );

	child = new t_bitmap_layer_window( m_layout->find( (layer_name + "_released").c_str() ), -location, 
		                               tab );
	text_window = new t_text_window( font, tab_rect, child, label.c_str(),
		                             t_pixel_24(0,0,0) );
	text_window->set_center_horizontal();
	text_window->set_drop_shadow( true, t_pixel_24( 200, 200, 200 ));
	tab->set_released_image( child );
	tab->set_pressed( pressed );
	set_help( tab, campaign_help, help_text.c_str() );

	return tab;
}

// ------------------------------------------------
// dialog to select a new map.
// add a tab
// ------------------------------------------------
void t_scenario_dialog::add_tab( const char* tab_name, std::string const& label, 
							    std::string const& help_text,
								void (t_scenario_dialog::*function)(t_button*),
								bool pressed )
{
	t_toggle_button* tab = add_tab( tab_name, label, help_text, pressed );
	if( tab == 0 )
		return;
	tab->set_click_handler( bound_handler( *this, function ) );
	m_tabs.add( tab );
}

// ---------------------------------------------------------------------
// add a text item
// ---------------------------------------------------------------------
t_text_window* t_scenario_dialog::add_text( std::string const& text, std::string const& layer_name, 
							               int size, bool size_explicit, bool center, t_window* const parent )
{
	t_bitmap_layer const* layer;
	t_screen_rect         rect;
	t_text_window*        text_window;
	t_cached_font         font;
	t_window*			  owner;

	layer = m_layout->find( layer_name );
	if( !layer )
		return 0;
	rect = layer->get_rect();
	if( size_explicit )
		font = get_font( size );
	else
		font = get_font( rect.height() / size );
	if( parent )
		owner = parent;
	else
		owner = this;
	text_window = new t_text_window( font, rect, owner, text, t_pixel_24(0,0,0));
	if( center )
		text_window->set_center_horizontal();
	text_window->set_drop_shadow( true, t_pixel_24(200,200,200));
	return text_window;
}

// -----------------------------------------------------------------------
// 
// -----------------------------------------------------------------------
void t_scenario_dialog::begin_clicked( t_button* button )
{
	set_music_volume( g_old_music_volume );
	if( m_sound )
		m_sound->fade_out();
	close();	// close all child windows too
	m_return_value = true;
}

// -----------------------------------------------------------------------
// 
// -----------------------------------------------------------------------
void t_scenario_dialog::cancel_clicked( t_button* button )
{
	set_music_volume( g_old_music_volume );
	if( m_sound )
		m_sound->fade_out();
	close();	// close all child windows too
}

// ------------------------------------------------
// dialog to select a new map.
// find location to place a control
// ------------------------------------------------
t_screen_point t_scenario_dialog::find_location( std::string const& layer_name ) const
{
	t_bitmap_layer const* layer = m_layout->find( layer_name.c_str() );

//	return to_client( layer->get_rect().top_left() );
	return layer->get_rect().top_left();
}

// ---------------------------------------------------------------------
// create buttons
// ---------------------------------------------------------------------
extern t_external_string const k_text_cancel;
static t_external_string const k_text_begin( "begin" );
static t_external_string const k_text_play_this_map( "play_map" );
extern t_external_string const k_text_next;
void t_scenario_dialog::create_buttons()
{
	t_screen_point			point(0,0);
	t_screen_rect			rect;
	t_button*				button;
	t_help_block const&		help = get_help_block( "shared" );
	t_help_block const&		campaign_help = get_help_block( "campaign" );

	// add cancel button
	if( m_is_prologue )
	{
		button = new t_button( g_large_button.get(), find_location( "back" ), this, k_text_cancel );
		button->set_click_handler( bound_handler(*this, &t_scenario_dialog::cancel_clicked ) );
		set_help( button, help, "cancel" );
	
		// add begin button
		button = new t_button( g_large_button.get(), find_location( "begin" ), this, k_text_begin );
		button->set_click_handler( bound_handler( *this, &t_scenario_dialog::begin_clicked ) );
		set_help( button, campaign_help, "begin" );
	} else
	{
		// add next button
		button = new t_button( g_large_button.get(), find_location( "next" ), this, k_text_next );
		button->set_click_handler( bound_handler( *this, &t_scenario_dialog::cancel_clicked ) );
		set_help( button, help, "next" );
	}

	// create replay button
	button = create_button( m_layout, "replay", this, point );
	button->set_click_handler( bound_handler( *this, &t_scenario_dialog::replay_clicked ));
	set_help( button, campaign_help, "replay_button" );

	// create pause button
	t_toggle_button* toggle_button;

	toggle_button = new t_toggle_button( find_location( "pause_released" ), this );
	set_button_layers( toggle_button, m_layout, "pause" );
	toggle_button->set_click_handler( bound_handler( *this, &t_scenario_dialog::pause_clicked ));
	toggle_button->set_double_click_handler( bound_handler( *this, &t_scenario_dialog::pause_clicked ));
	set_help( toggle_button, campaign_help, "pause_button" );
}

// ---------------------------------------------------------------------
// create icons
// ---------------------------------------------------------------------
void t_scenario_dialog::create_icons()
{
	t_bitmap_layer const*	layer;
	t_window*				window;
	t_screen_point			point(0,0);
	t_screen_rect			rect;
	t_help_block const&		campaign_help = get_help_block( "campaign" );
	t_help_block const&		shared_help = get_help_block( "shared" );
	t_map_header const &	header = m_file_header_ptr->get_map_header( m_map_selected );

	t_screen_point origin = -m_background->get_screen_rect().top_left();

	// create the appropriate picture for selected campaign
	t_scenario_cut_scene_image_id id;
	if( m_is_prologue )
		id = header.prologue_info.get_image_id();
	else
		id = header.epilogue_info.get_image_id();

	// Specifying a campaign image is not required
	if( id >= 0 && id < k_scenario_cut_scene_image_count )
	{
		m_layout_splashscreen = get_cache(id).get();
		rect = m_layout->find( "picture" )->get_rect();
		layer = m_layout_splashscreen->find( "frame 001" );
		window = new t_bitmap_layer_window( layer, rect.top_left(), this );
	}

	// icons
	layer = m_layout->find( "picture_frame" );
	if( layer )
		window = new t_bitmap_layer_window( layer, point, this );

	// title background
	layer = m_layout->find( "title_background" );
	if( layer )
		window = new t_bitmap_layer_window( layer, point, this );

	// win/loss box
	layer = m_layout->find( "win_loss_box" );
	if( layer )
		window = new t_bitmap_layer_window( layer, point, this );

	// carry over box
	layer = m_layout->find( "carryover_box" );
	if( layer )
		window = new t_bitmap_layer_window( layer, point, this );

	// player difficulty box
	layer = m_layout->find( "player_difficulty_box" );
	if( layer )
	{
		m_player_difficulty_box = new t_bitmap_layer_window( layer, point, m_background );

		point = m_layout->find("player_difficulty")->get_rect().top_left()
			- layer->get_rect().top_left();
		m_player_difficulty_window = new t_bitmap_group_window( m_difficulty_icons, point, 0,
										m_player_difficulty_box );
		set_help ( m_player_difficulty_window, campaign_help, "player_difficulty");
	}

	// gaurds move toggle button
	if( m_is_prologue )
	{
		t_toggle_button* toggle_button;
		t_button_handler	handler;

		m_guards_can_move = get_guard_move_preference();
		toggle_button = new t_toggle_button( find_location( "creature_guard_released" ), this );
		set_button_layers( toggle_button, m_layout, "creature_guard" );
		toggle_button->set_pressed( m_guards_can_move );
		if (m_guards_can_move)
			set_help( toggle_button, shared_help, "guards_pressed" ); 
		else
			set_help( toggle_button, shared_help, "guards_released" ); 

		handler = bound_handler( *this, &t_scenario_dialog::guard_clicked );
		toggle_button->set_click_handler( handler );   

		if( m_map_selected != 0 )
			toggle_button->enable( false );

		// map size icon
		int size = header.size;

		if (size <= 76)
			size = 0;
		else 
		if (size <= 152)
			size = 1;
		else 
		if (size <= 228)
			size = 2;
		else
			size = 3;
		point = m_layout->find("map_size")->get_rect().top_left();
		layer = m_map_size_icons->find( k_map_size_icon_names[size] );
		window = new t_bitmap_layer_window( layer, point, this );
		set_help ( window, campaign_help, k_map_size_keywords[size] );
	}
}

// ---------------------------------------------------------------------
// create all the text to go in each text box
// ---------------------------------------------------------------------
void t_scenario_dialog::create_text()
{
	t_screen_point			origin(0,0);
	t_bitmap_layer const*	layer;
	t_screen_rect			rect;
	std::string title;
	std::string voice_text;
	t_map_header const &	header = m_file_header_ptr->get_map_header( m_map_selected );
	t_text_window*			window;


	title = header.name;
	if( m_is_prologue )
		voice_text = header.prologue_info.get_text();
	else
		voice_text = header.epilogue_info.get_text();

	// add title
	if( !title.empty() )
	{
		layer = m_layout->find( "title" );
		rect = layer->get_rect();
		window = new t_text_window( get_font( rect.height() ), rect, this, 
									 title, t_pixel_24(0,0,0) );
		window->set_center_horizontal();
		window->set_center_vertical();
		window->set_drop_shadow( true, t_pixel_24(200,200,200));
	}

	// add voice over text
	if( !voice_text.empty() )
	{
		layer = m_layout->find( "voice_text" );
		rect = layer->get_rect();
		m_voice_window = new t_scrolling_text_window( get_font( 28 ), rect, this, 
									 voice_text, t_pixel_24(0,0,0), m_sound_length * 1000 );
		m_voice_window->set_scroll_position(k_onscreen_start_offset);
	}

	// add a text description for guards
	if( m_is_prologue )
	{
		rect = m_layout->find( "creature_guard_text" )->get_rect() + origin;
		m_guards_can_move_window = new t_text_window( get_font( rect.height() / 2 ), rect, m_background,
										 "", t_pixel_24(0,0,0) );
		m_guards_can_move_window->set_drop_shadow( true, t_pixel_24(200,200,200) );
		m_guards_can_move_window->set_center_horizontal();
		if (m_guards_can_move)
			m_guards_can_move_window->set_text( k_text_guards_move );
		else
			m_guards_can_move_window->set_text( k_text_guards_dont_move );

		// add a text description for player difficulty
		rect = m_layout->find( "player_difficulty_text" )->get_rect() + origin;
		m_difficulty_window = new t_text_window( get_font( rect.height() / 2 ), rect, m_background, "", t_pixel_24(0,0,0) );
		m_difficulty_window->set_drop_shadow( true, t_pixel_24(200,200,200) );
		m_difficulty_window->set_center_horizontal();

		set_difficulty( m_difficulty );
	}
}

// ---------------------------------------------------------------------
//
// ---------------------------------------------------------------------
static t_external_string const k_campaign_info_tab( "campaign_info.campaign" );
static t_external_string const k_scenario_info_tab( "scenario_info.campaign" );
static t_external_string const k_scenario_details_tab( "scenario_details.campaign" );
void t_scenario_dialog::initialize()
{

	t_map_header const & header = m_file_header_ptr->get_map_header( m_map_selected );

	// If this is not the first map, take the difficulty from the header
	if( m_map_selected > 0 && m_adventure_map )
		m_difficulty = m_adventure_map->get_player_difficulty();

	g_old_music_volume = get_music_volume();
	int volume = ( get_sound_volume() - 700 );
	if( volume < -10000 )
		volume = -10000;
	set_music_volume( minimum( volume, g_old_music_volume ) );
	
	play_music( k_main_menu_music );

	init_voice_over();

	assert( m_file_header_ptr != 0 );

	if( m_is_prologue )
		m_layout = g_layout.get();			// Regular campaign info
	else
		m_layout = g_layout_epilogue.get();	// For showing campaign epilogues

	if( m_is_prologue )
	{
		m_difficulty_icons = g_difficulty_icons.get();
		m_map_size_icons = g_map_size_icons.get();
	}

	// add background
	m_background = new t_bitmap_layer_window( m_layout->find( "background" ), 
		                                      t_screen_point(0,0), this);
	m_background->set_drop_shadow( true );

	//////////////////////////////////////////////////////////////////
 	// find dimensions & center
	t_screen_rect		  rect = m_layout->get_rect();

	rect += t_screen_point( (get_parent()->get_width() - rect.width()) / 2, 
		                           (get_parent()->get_height() - rect.height()) / 2 );
	init( rect );

	if( m_is_prologue )
	{
		add_tab( "tab_2", k_scenario_info_tab, "scenario_info_tab", 
				&t_scenario_dialog::scenario_info_clicked, false );

		add_tab( "tab_3", k_scenario_details_tab, "scenario_details_tab", 
				&t_scenario_dialog::scenario_details_clicked, true );
	}

	create_icons();
	create_text();
	create_buttons();

	if( m_is_prologue )
	{
		create_scenario_info_controls();
		create_scenario_details_controls();

		m_scenario_info_controls->set_visible( true );
		m_scenario_details_controls->set_visible( false );
	}
}

// ---------------------------------------------------------------------
//
// ---------------------------------------------------------------------
static t_external_string const k_victory( "win_condition.campaign" );
static t_external_string const k_loss( "loss_condition.campaign" );
static t_external_string const k_map_difficulty( "map_difficulty.campaign" );
static t_external_string const k_text_carryover( "carryover.campaign" );
void t_scenario_dialog::create_scenario_details_controls()
{
//	t_button*				button;
//	t_bitmap_layer const*	layer;
//	t_window*				window;
	t_screen_point			origin(0,0);
	t_help_block const&		campaign_help = get_help_block( "campaign" );
	t_map_header const & header = m_file_header_ptr->get_map_header( m_map_selected );

	m_scenario_details_controls = new t_window( get_client_rect(), k_completely_transparent,
		                                this );

	std::string text;

	text = k_victory;
	text += ":   ";
	text += header.victory_condition;
	text += "\n";

	text += k_loss;
	text += ":   ";
	text += header.loss_condition;
	text += "\n";

	text += k_map_difficulty;
	text += ":   ";
	text += k_difficult_balloon_help[header.difficulty].c_str();
	text += "\n";

	text += k_text_carryover;
	text += ":   ";
	text += header.carryover_description.c_str();
	add_text( text, "map_description", 16, true, false, m_scenario_details_controls );
}

// ---------------------------------------------------------------------
//
// ---------------------------------------------------------------------
void t_scenario_dialog::create_scenario_info_controls()
{
	t_map_header const & header = m_file_header_ptr->get_map_header( m_map_selected );

	m_scenario_info_controls = new t_window( get_client_rect(), k_completely_transparent,
		                                this );

	// add text
	// get the header for the first map
	add_text( header.description.c_str(), "map_description", 16, true, false, m_scenario_info_controls );
}

// ---------------------------------------------------------------------
// scenario info click handler
// ---------------------------------------------------------------------
void t_scenario_dialog::scenario_info_clicked( t_button* button )
{
	if (!button->is_pressed())
		return;
	m_scenario_info_controls->set_visible( true );
	m_scenario_details_controls->set_visible( false );
}

// ---------------------------------------------------------------------
// scenario details click handler
// ---------------------------------------------------------------------
void t_scenario_dialog::scenario_details_clicked( t_button* button )
{
	if (!button->is_pressed())
		return;
	m_scenario_info_controls->set_visible( false );
	m_scenario_details_controls->set_visible( true );
}

// --------------------------------------------------------
// initialize the voice over text and sound
// --------------------------------------------------------
void t_scenario_dialog::init_voice_over()
{
	play_voice_over_sound();
}

// --------------------------------------------------------
// pause button on voice over
// --------------------------------------------------------
void t_scenario_dialog::pause_clicked( t_button* button )
{
	m_paused = button->is_pressed();

	if( m_paused  )
	{
		if( !m_suspended )
		{
			if( m_sound )
				m_sound->stop( false );

			if (m_voice_window->is_active() == true)
				m_voice_window->suspend_idle_processing();
			
			m_suspended = true;
		} else
			button->set_pressed( true );
	} else
	{
		if( m_suspended )
		{
			if( m_sound && !m_sound->is_playing() )
				m_sound->play( 0 );

			if (m_voice_window->is_active() == false)
				m_voice_window->resume_idle_processing();

			m_suspended = false;
		} else
			button->set_pressed( false );
	}
}

// --------------------------------------------------------
// replay button on voice over
// --------------------------------------------------------
void t_scenario_dialog::replay_clicked( t_button* button )
{
	if( m_paused )	// if it's paused, they must unpause it first
		return;

	m_voice_window->set_scroll_position(k_onscreen_start_offset);
	m_voice_window->set_elapsed_time( 0 );

	if (m_voice_window->is_active() == false)
		m_voice_window->resume_idle_processing();

	if ( m_sound )
		m_sound->stop();

	init_voice_over();
}

// --------------------------------------------------------
// play a voice over sound
// --------------------------------------------------------
void t_scenario_dialog::play_voice_over_sound()
{
	t_map_header const &	header = m_file_header_ptr->get_map_header( m_map_selected );

	if ( m_sound )
		m_sound->stop();
	
	std::string name = "voice_over.";

	t_scenario_cut_scene_voice_over_id id;
	if( m_is_prologue )
		id = header.prologue_info.get_voice_over_id();
	else
		id = header.epilogue_info.get_voice_over_id();
	
	// The voice over sound doesn't HAVE to be set, it can be optionally left out
	if( id >= 0 && id < k_scenario_cut_scene_voice_over_count )
	{
		name += get_resource_name( id );

		t_sound_cache cache;
		cache = t_sound_cache( name );

		t_sound_ptr sound = cache.get();
		//== writen by sunjun ==//
		if( sound != 0 )
		{
			if( sound->get() )
			{
				m_sound = sound->play( 0 );

				t_abstract_sound const* sound_data = sound->get();
				m_sound_length = (int)(double( sound_data->get_size() ) / (sound_data->get_bits_per_sample() 
											* sound_data->get_channels()
											* sound_data->get_samples_per_second() / 8));
			}			
		}
	}
}

// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
void t_scenario_dialog::guard_clicked( t_button* button )
{
	t_help_block const&		shared_help = get_help_block( "shared" );

	m_guards_can_move = button->is_pressed();
	set_guard_move_preference( m_guards_can_move );
	if (m_guards_can_move)
	{
		m_guards_can_move_window->set_text( k_text_guards_move );
		set_help( button, shared_help, "guards_pressed" ); 
	} else
	{
		m_guards_can_move_window->set_text( k_text_guards_dont_move );
		set_help( button, shared_help, "guards_released" ); 
	}
}


// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------


// ---------------------------------------------------------------------
// dialog to show multi-scenario info
// ---------------------------------------------------------------------
t_multi_scenario_dialog::t_multi_scenario_dialog(	t_window* parent, 
													t_multi_scenario_campaign_file_header_ptr const header_ptr, 
													int map_selected,
													bool is_epilogue,
													t_adventure_map* map ) :
													t_scenario_dialog( parent, header_ptr, map_selected, is_epilogue, map ),
													m_file_header_ptr( header_ptr )
{
	initialize();
}

// ------------------------------------------------
// add a tab
// ------------------------------------------------
void t_multi_scenario_dialog::add_tab( const char* tab_name, std::string const& label, 
							    std::string const& help_text,
								void (t_multi_scenario_dialog::*function)(t_button*),
								bool pressed )
{
	t_toggle_button* tab = t_scenario_dialog::add_tab( tab_name, label, help_text, pressed );
	tab->set_click_handler( bound_handler( *this, function ) );
	m_tabs.add( tab );
}

// ---------------------------------------------------------------------
// campaign info click handler
// ---------------------------------------------------------------------
void t_multi_scenario_dialog::campaign_info_clicked( t_button* button )
{
	if (!button->is_pressed())
		return;
	m_campaign_info_controls->set_visible( true );
	m_scenario_info_controls->set_visible( false );
	m_scenario_details_controls->set_visible( false );
}

void t_multi_scenario_dialog::create_buttons()
{
	t_screen_point			point(0,0);
	t_screen_rect			rect;
	t_help_block const&		help = get_help_block( "shared" );
	t_help_block const&		campaign_help = get_help_block( "campaign" );

	if( m_is_prologue && m_map_selected == 0 )
	{
		// create player difficulty left button
		m_difficulty_left_button = create_button( m_layout, "left", this, point );
		m_difficulty_left_button->set_click_handler( bound_handler( *this, &t_multi_scenario_dialog::player_difficulty_left_clicked ) );
		set_help( m_difficulty_left_button, campaign_help, "player_difficulty_left" );

		// create player difficulty right button
		m_difficulty_right_button = create_button( m_layout, "right", this, point );
		m_difficulty_right_button->set_click_handler( bound_handler( *this, &t_multi_scenario_dialog::player_difficulty_right_clicked ) );
		set_help( m_difficulty_right_button, campaign_help, "player_difficulty_right" );

		set_difficulty( m_difficulty );
	}
}

// ---------------------------------------------------------------------
// create the campaign info controls
// ---------------------------------------------------------------------
void t_multi_scenario_dialog::create_campaign_info_controls()
{
	t_screen_point			origin(0,0);
	t_help_block const&		help = get_help_block( "campaign" );
	t_map_header const &	header = m_file_header_ptr->get_map_header( m_map_selected );

	m_campaign_info_controls = new t_window( get_client_rect(), k_completely_transparent, this );

	std::string text;

	// add text
	text = m_file_header_ptr->get_description();
	add_text( text, "map_description", 16, true, false, m_campaign_info_controls );
}

void t_multi_scenario_dialog::create_icons()
{
}

void t_multi_scenario_dialog::create_text()
{
}

void t_multi_scenario_dialog::initialize()
{
	// create the tabs
	if( m_is_prologue )
	{
		add_tab( "tab_1", k_campaign_info_tab, "campaign_info_tab", 
				&t_multi_scenario_dialog::campaign_info_clicked, false );
	}

	create_icons();
	create_text();
	create_buttons();

	if( m_is_prologue )
	{
		create_campaign_info_controls();

		m_campaign_info_controls->set_visible( false );
		m_scenario_info_controls->set_visible( false );
		m_scenario_details_controls->set_visible( true );
	}
}

// ---------------------------------------------------------------------
// click handler
// ---------------------------------------------------------------------
void t_multi_scenario_dialog::player_difficulty_left_clicked( t_button* button )
{
	enum_decr( m_difficulty );
	if( m_difficulty < 0 )
		m_difficulty = k_difficulty_easy; 

	set_difficulty( m_difficulty );
	if( m_adventure_map )
		m_adventure_map->set_player_difficulty( m_difficulty );
}

// ---------------------------------------------------------------------
// click handler
// ---------------------------------------------------------------------
void t_multi_scenario_dialog::player_difficulty_right_clicked( t_button* button )
{
	enum_incr( m_difficulty );
	if( m_difficulty >= k_difficulty_count )
		m_difficulty = k_difficulty_impossible; 

	set_difficulty( m_difficulty );
	if( m_adventure_map )
		m_adventure_map->set_player_difficulty( m_difficulty );
}

// ---------------------------------------------------------------------
// scenario info click handler
// ---------------------------------------------------------------------
void t_multi_scenario_dialog::scenario_info_clicked( t_button* button )
{
	if (!button->is_pressed())
		return;
	m_campaign_info_controls->set_visible( false );
	m_scenario_info_controls->set_visible( true );
	m_scenario_details_controls->set_visible( false );
}

// ---------------------------------------------------------------------
// scenario details click handler
// ---------------------------------------------------------------------
void t_multi_scenario_dialog::scenario_details_clicked( t_button* button )
{
	if (!button->is_pressed())
		return;
	m_campaign_info_controls->set_visible( false );
	m_scenario_info_controls->set_visible( false );
	m_scenario_details_controls->set_visible( true );
}


